using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Threading;

using KNCApp.Business;
using KNCApp.Business.Models;
using KNCApp.Business.Interfaces;

namespace KNCApp.Port
{
    public class ResultsReader : IReceiver
    {
        public static TestState State = TestState.Stop;
        
        private PortHandler portHandler;
        private IDataParser parser;
        private List<IDataHandler> handlers;
        private DateTime resDT = DateTime.MinValue;

        public  TestPilot testPilot;


        public ResultsReader(TestPilot tp)
        {
            testPilot = tp;
            init();
            
        }

        private void init()
        {
            handlers = new List<IDataHandler>();
            portHandler = new PortHandler(SettingsMgr.GetPort(testPilot.Position));
        }

        public void RegisterParser(IDataParser dp)
        {
            parser = dp;
        }

        public void RegisterHandler(IDataHandler dh)
        {
            handlers.Add(dh);
        }

        public void Receive(string data)
        {
            Program.LogDebug("New data received - " + data);
            if (parser != null)
            {
                AbstractObj obj = parser.Parse(data);
                if (obj != null)
                {
                    Result res = obj as Result;
                    if (res != null)
                    {
                        res.TestPilot = testPilot;
                        if (resDT == DateTime.MinValue)
                        {
                            resDT = DateTime.Now;
                        }
                        else
                        {
                            resDT = resDT.AddSeconds(1);
                        }
                        res.Time = resDT;
                        res.Pressure = PressureReader.Pressure;
                        res.State = State;
                        res.Tlen = testPilot.Tlen;
                    }
                    else
                    {
                        ChartResult cr = obj as ChartResult;
                        if (cr != null)
                        {
                            cr.TestPilot = testPilot;
                        }
                    }
                    foreach (IDataHandler h in handlers)
                    {
                        h.HandleData(obj);
                    }
                }
            }
        }

        public void Start()
        {
            if (portHandler != null)
            {
                if (parser != null)
                {
                    portHandler.StartReading(parser.GetMode(), this);
                }
                else
                {
                    portHandler.StartReading("", this);
                }
            }
        }

        public void Stop()
        {
            if(portHandler != null)
                portHandler.Stop("x");
        }
    }
}
